package com.diaohw.module.system.service.impl;

import com.diaohw.module.system.mapper.AccountRoleMapper;
import com.diaohw.module.system.obj.entity.AccountRole;
import com.diaohw.module.system.service.AccountRoleService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountRoleImpl implements AccountRoleService {

    @Resource
    private AccountRoleMapper accountRoleMapper;


    @Override
    public List<AccountRole> list(List<String> accountIds) {
        QueryWrapper wrapper = QueryWrapper.create();
        wrapper.in(AccountRole::getAccountId, accountIds);
        return accountRoleMapper.selectListByQuery(wrapper);
    }


    @Override
    public void resetRole(String accountId, List<String> roleIds) {
        accountRoleMapper.deleteByQuery(QueryWrapper.create().eq(AccountRole::getAccountId, accountId));
        if (roleIds == null || roleIds.isEmpty()) {
            return;
        }
        List<AccountRole> list = roleIds.stream().map(roleId -> AccountRole.of(accountId, roleId)).toList();

        accountRoleMapper.insertBatch(list);

    }

}
